﻿// The MIT License (MIT)
//
// Copyright (c) 2015 FramTack, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
// For more information visit the project home page at http://solutionfamily.codeplex.com or the owner's site at http://solution-family.com
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace SolutionFamily.IO
{
    public class J1939Parameter
    {
        private object m_value;

        internal J1939Parameter()
        {
            Scale = 1.0;
            Offset = 0.0;
            DataLength = 1;
            DataOffset = 0;
            Name = "<Undefined>";
            SPN = -1;
            DataType = typeof(double);
            Writable = false;
        }

        public int SPN { get; internal set; }
        public string Name { get; internal set; }
        public string Units { get; internal set; }
        public Type DataType { get; set; }
        public bool Writable { get; set; }
        public double MinValue { get; set; }
        public double MaxValue { get; set; }

        internal int DataOffset { get; set; }
        internal int DataLength { get; set; }
        internal double Scale { get; set; }
        internal double Offset { get; set; }

        public object Value 
        {
            get { return m_value; }
            set
            {
                switch (Type.GetTypeCode(this.DataType))
                {
                    case TypeCode.Byte:
                    case TypeCode.Int16:
                    case TypeCode.UInt16:
                    case TypeCode.Int32:
                    case TypeCode.UInt32:
                        if (value == null)
                        {
                            m_value = 0;
                        }
                        else
                        {
                            if(value is byte)
                            {
                                m_value = value;
                            }
                            else if (value is int)
                            {
                                m_value = value;
                            }
                            if (value is uint)
                            {
                                m_value = value;
                            }
                            if (value is short)
                            {
                                m_value = value;
                            }
                            if (value is ushort)
                            {
                                m_value = value;
                            }
                            else
                            {
                                var tmp = value.ToString();
                                var index = tmp.IndexOf('.');

                                if (index >= 0)
                                {
                                    m_value = Convert.ToUInt32(tmp.Substring(0, index));
                                }
                                else
                                {
                                    m_value = Convert.ToUInt32(tmp);
                                }
                            }
                        }
                        break;
                    case TypeCode.Double:
                    case TypeCode.Single:
//                        break;
                    default:
                        m_value = value;
                        break;
                }
            }
        }

        internal J1939Parameter Copy()
        {
            return (J1939Parameter)this.MemberwiseClone();
        }
    }
}
